package com.example.thinkpad.payby3;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.example.thinkpad.GuideActivity;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //http://localhost:8080/HeiMaPay/Pay?goodId=111&count=2&price=0.01
    //商品服务器地址
    private String baseUrl = "http://192.168.33.27:8080/HeiMaPay/";
    private Button mButton;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //结果
            Map<String,String> result= (Map<String, String>) msg.obj;

            String memo = result.get("memo");//描述信息
            String status = result.get("result");//结果
            String resultStatus = result.get("resultStatus");//结果码
            Toast.makeText(MainActivity.this,resultStatus,Toast.LENGTH_SHORT).show();

        }
    };
    private ImageButton mBtn_img;
    private Button mBtn_viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.btn);
        mBtn_viewpager = (Button) findViewById(R.id.btn_viewpager);
        mBtn_viewpager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,GuideActivity.class);
                startActivity(intent);
            }
        });
        mButton.setOnClickListener(this);

        mBtn_img = (ImageButton) findViewById(R.id.btn_img);
        Drawable icon = getResources().getDrawable(R.mipmap.pay);
        Drawable wrap = DrawableCompat.wrap(icon);
        DrawableCompat.setTintList(wrap,getResources().getColorStateList(R.color.pay_selector));
//        DrawableCompat.setTintList(wrap, ColorStateList.valueOf(Color.RED));
        mBtn_img.setImageDrawable(wrap);

        mBtn_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"DrawableCompat:一张图片实现selector",Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void onClick(View v) {
        new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiService.class)
                .submit("111", "2", "0.01")
                .enqueue(new Callback<OrderInfo>() {
                    @Override
                    public void onResponse(Call<OrderInfo> call, Response<OrderInfo> response) {
                        if (response.isSuccessful()) {
                            //订单信息
                            OrderInfo body = response.body();
                            Toast.makeText(MainActivity.this, body.toString(), Toast.LENGTH_SHORT).show();

                            startPay(body.getPayInfo());

                        }
                    }

                    @Override
                    public void onFailure(Call<OrderInfo> call, Throwable t) {
                        Log.d("TAG",t.toString());
                        Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
                    }
                });
    }

    private void startPay(String payinfo) {
        final String orderInfo = payinfo;
        new Thread(new Runnable() {
            @Override
            public void run() {
                PayTask payTask = new PayTask(MainActivity.this);
                Map<String, String> result = payTask.payV2(orderInfo, true);

                Message msg = new Message();
                msg.obj = result;
                msg.what = 0;
                mHandler.sendMessage(msg);
            }
        }).start();
    }

    public interface ApiService{
        @GET("Pay")
        Call<OrderInfo> submit(
                @Query("goodId") String goodId,
                @Query("count") String count,
                @Query("price") String price);
    }

}
